package com.codeforces.contest.c20;
import java.util.Iterator;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;
import java.util.Vector;


public class _20C {

	static Scanner scanner = new Scanner(System.in);
	static int N, M, from, to, we;
	static Vector<ele>[] vs;

	public static void main(String[] args) {
		N = scanner.nextInt();
		M = scanner.nextInt();
		vs = new Vector[N+1];
		for (int i=1;i<=N;++i) {
			vs[i] = new Vector<ele>();
		}
		for (int i=1;i<=M;++i) {
			from = scanner.nextInt();
			to = scanner.nextInt();
			we = scanner.nextInt();
			vs[from].add(new ele(to,we));
			vs[to].add(new ele(from,we));
		}
		
		boolean suc = dijkstra();
		if (suc) {
			Stack<Integer> sta = new Stack<Integer>();
			int child = N;
			do {
				sta.add(child);
				child = parent[child];
			} while (child != 1);
			sta.add(child);
			while (!sta.isEmpty()) {
				System.out.print(sta.pop());
				if (!sta.isEmpty()) {
					System.out.print(" ");
				}
				else {
					System.out.println();
				}
			}
		}

	}

	static long[] label;
	static int[] visit;
	static int[] parent;
	static Queue<hnode> qu = new PriorityQueue<hnode>();
	
	private static boolean dijkstra() {
		label = new long[N+1];
		visit = new int[N+1];
		parent = new int[N+1];
		for (int i=1;i<=N;++i) {
			label[i] = Long.MAX_VALUE;
			parent[i] = -1;
		}
		label[1] = 0;
		qu.add(new hnode(-label[1],1));
		Iterator<ele> it;
		while (true) {
			if (qu.isEmpty()) {
				System.out.println(-1);
				return false;
			}
			else {
				hnode top = qu.poll();
				visit[top.idx] = 1;
				if (top.idx == N) {
					break;
				}
				else {
					it = vs[top.idx].iterator();
					while (it.hasNext()) {
						ele nxt = it.next();
						if (visit[nxt.to] == 0) {
							if (label[nxt.to] > label[top.idx] + nxt.len) {
								label[nxt.to] = label[top.idx] + nxt.len;
								qu.add(new hnode(label[top.idx] + nxt.len, nxt.to));
								parent[nxt.to] = top.idx;
							}
						}
					}
				}
			}
		}
		return true;
	}

}
class hnode implements Comparable<hnode> {
	long we;
	int idx;
	hnode(long we, int idx) {
		this.we = we;
		this.idx = idx;
	}
	public int compareTo(hnode h) {
		return Long.compare(this.we,h.we);
	}
	
}
class ele {
	int to;
	int len;
	ele(int to, int len) {
		this.to = to;
		this.len = len;
	}
}
